package rmiserver;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import core.Controller;

import user.User;

import messages.Message;
import messages.QuitMessage;

public class RMIClientHandler extends UnicastRemoteObject implements ServerRemote {
	
	public RMIClientHandler(User user) throws RemoteException {
		this.controller = Controller.getInstance();
		System.out.println("New client logged");
		this.user = user;
	}

	
	public void send(Message message) throws RemoteException {
		if (message == null || controller == null)
			return;
				
		controller.getMessageManager().getMessageConsumer().putMessage(message, this.user);
		
		if (message instanceof QuitMessage) {
			controller = null; // The client shouldn't bother us anymore
			user = null;
		}
	}
	
	
	private User user;	// The user that this handler is servicing.
	protected Controller controller; 			// The main Controller
	
	private static final long serialVersionUID = -532767676056181913L;
}
